// 地区选择联动
var style = '<style>'+
                '.locationBox{'+
                    'position: fixed;'+
                    'top: 50%;'+
                    'left: 50%;'+
                    'transform: translate(-50%,-50%);'+
                    'background: #fff;'+
                    'width: 30%;'+
                    'padding: 0.5rem 0.2rem;'+
                    'height:10rem;'+
                    'border: #f0f0f0 solid 0.02rem;'+
                    'box-sizing: border-box;'+
                    'overflow-y: scroll;'+
                    'color: #666;'+
                '}'+
                '.locationBox li{'+
                    'padding: 0.5rem 0;'+
                '}'+
            '</style>'


// 省份点击
function provinceFun (locationObj) {
    $('.locationBox li').on('click', function () {
        var id = $(this).attr('data-id') 
        locationObj.pid = id

        if (locationObj.city) {
            cityAJAX (locationObj)
        } else {
            $(locationObj.clickTag).val($(this).text())
            changeInput()
        }
    })
}

// 市点击
function cityFun(locationObj) {
    $('.locationBox li').on('click', function () {
        var id = $(this).attr('data-id') 
        locationObj.pid = id

        if (locationObj.region) {
            regionAJAX (locationObj)
        } else {
            $(locationObj.clickTag).val($(this).text())
            changeInput()
            $('.locationBox').remove()
        }
    }) 
}

// 区点击
function regionFun(locationObj) {
    $('.locationBox li').on('click', function () {
        var id = $(this).attr('data-id') 
        locationObj.pid = id
        $(locationObj.clickTag).val($(this).text())
        changeInput()
        $('.locationBox').remove()
    })
}

// 省/市/区接口
    // 省

    function provinceAJAX(locationObj) {
        $.ajax({
            type:'post',
            url:'/api/home/locate/provice',
            data:{
                app_access: locationObj.app_access
            },
            success:function(res){
                
                var provinceHtml = ''
                for (var i = 0, len = res.data.length; i < len; i++) {
                    provinceHtml += '<li data-id="'+res.data[i].id+'">'+res.data[i].short_name+'</li>'
                }
                $(locationObj.tag).append(style + '<div class="locationBox"><ul>'+provinceHtml+'</ul></div>')

                provinceFun (locationObj)

                return res.data
            },
            error:function(){
                getErrMsg('操作失败！'); return;
            }
        });
    }

    // 市

    function cityAJAX(locationObj) {
        $.ajax({
            type:'post',
            url:'/api/home/locate/city',
            data:{
                app_access: locationObj.app_access,
                pid: locationObj.pid
            },
            success:function(res){
                
                var provinceHtml = ''
                for (var i = 0, len = res.data.length; i < len; i++) {
                    provinceHtml += '<li data-id="'+res.data[i].id+'">'+res.data[i].short_name+'</li>'
                }
                $(locationObj.tag).append(style + '<div class="locationBox"><ul>'+provinceHtml+'</ul></div>')

                cityFun (locationObj)

                return res.data
            },
            error:function(){
                getErrMsg('操作失败！'); return;
            }
        });
    }

    // 区

    function regionAJAX(locationObj) {
        $.ajax({
            type:'post',
            url:'/api/home/locate/region',
            data:{
                app_access: locationObj.app_access,
                pid: locationObj.pid
            },
            success:function(res){
                
                var provinceHtml = ''
                for (var i = 0, len = res.data.length; i < len; i++) {
                    provinceHtml += '<li data-id="'+res.data[i].id+'">'+res.data[i].short_name+'</li>'
                }
                $(locationObj.tag).append(style + '<div class="locationBox"><ul>'+provinceHtml+'</ul></div>')

                regionFun(locationObj)
                return res.data
            },
            error:function(){
                getErrMsg('操作失败！'); return;
            }
        });
    }